package classesInterface;

import classesBancodeDados.Paciente;
import classesBancodeDados.Responsavel;
import classesJpa.FichaInternamentoJpaController;
import classesJpa.ResponsavelJpaController;
import classesJpa.exceptions.NonexistentEntityException;
import classesMiolo.Formatador;
import classesMiolo.MapasFichaInternamento;
import classesMiolo.MapasPaciente;
import classesMiolo.ReverseMapper;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

public class TelaFichaInternamento extends javax.swing.JFrame {

    /**
     * Creates new form FichaInternamento
     */
    private TelaFichaInternamento() {
        initComponents();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    
    private static TelaFichaInternamento uniqueInstance;
    
    public static TelaFichaInternamento getInstance(){
        if(uniqueInstance == null){
            uniqueInstance = new TelaFichaInternamento();
        }
        return uniqueInstance;
    }
    
    public void limpaCampos(){
        campoApelido.setText("");
        campoCEP.setText("");
        campoCNS.setText("");
        campoCorPele.setText("");
        campoDataAlta.setText("");
        campoDataInternamento.setText("");
        campoEndereco.setText("");
        campoEnderecoPaciente.setText("");
        campoFiliacao.setText("");
        campoHora.setText("");
        campoLaudo.setText("");
        campoLeito.setText("");
        campoMunicipio.setText("");
        campoNascimento.setText("");
        campoNaturalidade.setText("");
        campoNome.setText("");
        campoNomeConjuge.setText("");
        campoNomeResponsavel.setText("");
        campoOrd.setText("");
        campoProfissao.setText("");
        campoProntuario.setText("");
        campoTelefone.setText("");
    }
    
    public void turnEditable(boolean editable){
        this.editable = editable;
        botaoSalvar.setEnabled(editable);
        campoApelido.setEditable(false);
        campoCEP.setEditable(false);
        campoCNS.setEditable(false);
        campoCorPele.setEditable(false);
        campoDataAlta.setEditable(editable);
        campoDataInternamento.setEditable(editable);
        campoEndereco.setEditable(false);
        campoEnderecoPaciente.setEditable(false);
        campoFiliacao.setEditable(false);
        campoHora.setEditable(editable);
        campoLaudo.setEditable(editable);
        campoLeito.setEditable(false);
        campoMunicipio.setEditable(false);
        campoNascimento.setEditable(false);
        campoNaturalidade.setEditable(false);
        campoNome.setEditable(false);
        campoNomeConjuge.setEditable(false);
        campoNomeResponsavel.setEditable(false);
        campoOrd.setEditable(editable);
        campoProfissao.setEditable(false);
        campoProntuario.setEditable(false);
        campoTelefone.setEditable(false);   
        comboCivil.setEnabled(false);
        comboSimNao.setEnabled(editable);
        comboSex.setEnabled(false);
        comboMotivo.setEnabled(editable);
    }
    
    public void preencherCampos(Paciente p, Responsavel r, classesBancodeDados.FichaInternamento fi){
        paciente = p;
        this.campoApelido.setText(p.getApelido());
        this.campoCEP.setText(p.getCep());
        this.campoCNS.setText(p.getSus());
        this.campoCorPele.setText(p.getCor());
        this.campoDataAlta.setText(fi.getDataAlta());
        this.campoDataInternamento.setText(fi.getDataInter());
        //this.campoEndereco.setText(r.getEndereco());
        this.campoEnderecoPaciente.setText(p.getEndLogradouro() + ", " + p.getEndNumero() + ", " + p.getEndBairro());
        this.campoFiliacao.setText(p.getNomePai() + "/" + p.getNomeMae());
        this.campoHora.setText(fi.getHoraInter());
        this.campoLaudo.setText(fi.getNumLaudo());
        //this.campoLeito.setText("" + fi.getLeito());
        this.campoMunicipio.setText(p.getCidade());
        this.campoNascimento.setText(p.getDataDeNascimento());
        this.campoNome.setText(p.getNome());
        this.campoNomeResponsavel.setText(r.getNome());
        this.campoOrd.setText(fi.getNumOrdArq());
        this.campoProfissao.setText(p.getProfissao());
        this.campoProntuario.setText(fi.getNumeroProntuario());
        this.campoTelefone.setText(p.getTelefone());
        MapasPaciente mp = MapasPaciente.getInstance();
        this.comboCivil.setSelectedItem(ReverseMapper.getKeyFromValue(mp.getEstadoCivil(), p.getEstadoCivil()));
        MapasFichaInternamento mfi = MapasFichaInternamento.getInstance();
        this.comboMotivo.setSelectedItem(ReverseMapper.getKeyFromValue(mfi.getMotivoAlta(), fi.getMotivoAlta()));
        this.comboSex.setSelectedItem(ReverseMapper.getKeyFromValue(mp.getSexo(), p.getSexo()));
        this.comboSimNao.setSelectedItem(fi.isOutroInter() ? "Sim" : "Não");
    }
    
    private void salvar(){
        ficha.setDataAlta(this.campoDataAlta.getText());
        ficha.setDataInter(this.campoDataInternamento.getText());
        ficha.setHoraInter(this.campoHora.getText());
        ficha.setNumLaudo(this.campoLaudo.getText());
        ficha.setNumOrdArq(this.campoOrd.getText());
        
        FichaInternamentoJpaController ic = new FichaInternamentoJpaController();
        try {
            ic.edit(ficha);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(TelaFichaInternamento.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TelaFichaInternamento.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        campoNome = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        campoProntuario = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        campoDataInternamento = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        campoDataAlta = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        campoLeito = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        comboSex = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        campoHora = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        campoOrd = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        campoFiliacao = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        campoNascimento = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        campoNaturalidade = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        campoProfissao = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        comboCivil = new javax.swing.JComboBox();
        jLabel15 = new javax.swing.JLabel();
        campoNomeConjuge = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        campoEnderecoPaciente = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        campoMunicipio = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        campoCEP = new javax.swing.JTextField();
        jLabel19 = new javax.swing.JLabel();
        campoTelefone = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        campoCorPele = new javax.swing.JTextField();
        jLabel21 = new javax.swing.JLabel();
        campoApelido = new javax.swing.JTextField();
        jLabel22 = new javax.swing.JLabel();
        comboSimNao = new javax.swing.JComboBox();
        jLabel23 = new javax.swing.JLabel();
        campoCNS = new javax.swing.JTextField();
        jLabel24 = new javax.swing.JLabel();
        campoLaudo = new javax.swing.JTextField();
        jLabel25 = new javax.swing.JLabel();
        campoNomeResponsavel = new javax.swing.JTextField();
        jLabel26 = new javax.swing.JLabel();
        campoEndereco = new javax.swing.JTextField();
        jLabel27 = new javax.swing.JLabel();
        comboMotivo = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        botaoSalvar = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setText("Ficha de Internamento");

        jLabel2.setText("Nome:");

        jLabel3.setText("Nº Prontuário:");

        jLabel4.setText("Data de Internamento:");

        campoDataInternamento.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                campoDataInternamentoKeyPressed(evt);
            }
        });

        jLabel5.setText("Data da Alta:");

        campoDataAlta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                campoDataAltaKeyPressed(evt);
            }
        });

        jLabel6.setText("Nº Leito:");

        jLabel7.setText("Sexo:");

        comboSex.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Masculino", "Feminino" }));

        jLabel8.setText("Hora da Internação:");

        campoHora.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                campoHoraKeyPressed(evt);
            }
        });

        jLabel9.setText("Nº Ord. Arquivo");

        jLabel10.setText("Filiação:");

        jLabel11.setText("Data de Nascimento:");

        campoNascimento.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                campoNascimentoKeyPressed(evt);
            }
        });

        jLabel12.setText("Naturalidade:");

        jLabel13.setText("Profissão:");

        jLabel14.setText("Estado Civil:");

        comboCivil.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Solteiro", "Casado", "Divorciado", "Viúvo" }));

        jLabel15.setText("Nome do Cônjuge:");

        jLabel16.setText("Endereço do Paciente:");

        jLabel17.setText("Município:");

        jLabel18.setText("CEP:");

        campoCEP.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                campoCEPKeyPressed(evt);
            }
        });

        jLabel19.setText("Telefone:");

        campoTelefone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoTelefoneActionPerformed(evt);
            }
        });
        campoTelefone.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                campoTelefoneKeyPressed(evt);
            }
        });

        jLabel20.setText("Cor da Pele:");

        jLabel21.setText("Apelido:");

        jLabel22.setText("Outros Internamentos:");

        comboSimNao.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Não", "Sim" }));

        jLabel23.setText("Nº do CNS:");

        jLabel24.setText("Nº Laudo:");

        jLabel25.setText("Responsável Pelo Paciente:");

        jLabel26.setText("Endereço do Responsável:");

        jLabel27.setText("Motivo da Alta:");

        comboMotivo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Médica", "Abandono", "Evasão/Fuga", "Pedido", "Transferência", "Óbito" }));

        jButton1.setText("Próximo >");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        botaoSalvar.setText("Salvar");
        botaoSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoSalvarActionPerformed(evt);
            }
        });

        jButton3.setText("Cancelar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoNome))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoProntuario, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoLeito)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoDataAlta))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoDataInternamento))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(comboSex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoHora, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoOrd))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoFiliacao))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel13)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoProfissao))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel12)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoNaturalidade))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel14)
                                .addGap(18, 18, 18)
                                .addComponent(comboCivil, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel15)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoNomeConjuge))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel16)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoEnderecoPaciente))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel17)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoMunicipio, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel18)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoCEP))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel25)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoNomeResponsavel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel26)
                        .addGap(22, 22, 22)
                        .addComponent(campoEndereco))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel22)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(comboSimNao, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel23)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(campoCNS))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel19)
                                        .addGap(18, 18, 18)
                                        .addComponent(campoTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel20)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(campoCorPele, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel21)
                                    .addComponent(jLabel24))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(campoApelido)
                                    .addComponent(campoLaudo, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel27)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(comboMotivo, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 0, Short.MAX_VALUE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                        .addGap(0, 0, Short.MAX_VALUE)
                                        .addComponent(jButton3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(botaoSalvar)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(campoProntuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(campoDataInternamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(campoDataAlta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(campoLeito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(comboSex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(campoHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(campoOrd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(campoFiliacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(campoNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12)
                    .addComponent(campoNaturalidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(campoProfissao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14)
                    .addComponent(comboCivil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(campoNomeConjuge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(campoEnderecoPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel17)
                    .addComponent(campoMunicipio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel18)
                    .addComponent(campoCEP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel19)
                    .addComponent(campoTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel20)
                    .addComponent(campoCorPele, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel21)
                    .addComponent(campoApelido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel22)
                    .addComponent(comboSimNao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel23)
                    .addComponent(campoCNS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel24)
                    .addComponent(campoLaudo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel25)
                    .addComponent(campoNomeResponsavel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel26)
                    .addComponent(campoEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel27)
                    .addComponent(comboMotivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(botaoSalvar)
                    .addComponent(jButton3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void campoDataInternamentoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoDataInternamentoKeyPressed
        if(evt.getKeyChar() != '\b'){
            campoDataInternamento.setText(Formatador.formataData(campoDataInternamento.getText()));
        }
    }//GEN-LAST:event_campoDataInternamentoKeyPressed

    private void campoDataAltaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoDataAltaKeyPressed
        if(evt.getKeyChar() != '\b'){
            campoDataAlta.setText(Formatador.formataData(campoDataAlta.getText()));
        }
    }//GEN-LAST:event_campoDataAltaKeyPressed

    private void campoHoraKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoHoraKeyPressed
        if(evt.getKeyChar() != '\b'){
            campoHora.setText(Formatador.formataHora(campoHora.getText()));
        }
    }//GEN-LAST:event_campoHoraKeyPressed

    private void campoNascimentoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoNascimentoKeyPressed
        if(evt.getKeyChar() != '\b'){
            campoNascimento.setText(Formatador.formataData(campoNascimento.getText()));
        }
    }//GEN-LAST:event_campoNascimentoKeyPressed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        TelaFichaInternamento2.getInstance().setFicha(ficha);
        ResponsavelJpaController resp = new ResponsavelJpaController();
        TelaFichaInternamento2.getInstance().preencherCampos(paciente, resp.findResponsavel(paciente.getCpfResp()), ficha);
        TelaFichaInternamento2.getInstance().turnEditable(editable);
        TelaFichaInternamento2.getInstance().setVisible(true);
        setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void botaoSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoSalvarActionPerformed
        salvar();
    }//GEN-LAST:event_botaoSalvarActionPerformed

    private void campoTelefoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoTelefoneActionPerformed

    }//GEN-LAST:event_campoTelefoneActionPerformed

    private void campoTelefoneKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoTelefoneKeyPressed
        if(evt.getKeyChar() != '\b'){
            campoTelefone.setText(Formatador.formataTelefone(campoTelefone.getText()));
        }
    }//GEN-LAST:event_campoTelefoneKeyPressed

    private void campoCEPKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoCEPKeyPressed
        if(evt.getKeyChar() != '\b'){
            campoCEP.setText(Formatador.formataCEP(campoCEP.getText()));
        }
    }//GEN-LAST:event_campoCEPKeyPressed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        TelaFichaInternamento.getInstance().limpaCampos();
        TelaFichaInternamento2.getInstance().limpaCampos();
        setVisible(false);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        TelaFichaInternamento.getInstance().limpaCampos();
        TelaFichaInternamento2.getInstance().limpaCampos();
    }//GEN-LAST:event_formWindowClosing

    public classesBancodeDados.FichaInternamento getFicha() {
        return ficha;
    }

    public void setFicha(classesBancodeDados.FichaInternamento ficha) {
        this.ficha = ficha;
    }   
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaFichaInternamento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaFichaInternamento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaFichaInternamento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaFichaInternamento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                TelaFichaInternamento.getInstance().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoSalvar;
    private javax.swing.JTextField campoApelido;
    private javax.swing.JTextField campoCEP;
    private javax.swing.JTextField campoCNS;
    private javax.swing.JTextField campoCorPele;
    private javax.swing.JTextField campoDataAlta;
    private javax.swing.JTextField campoDataInternamento;
    private javax.swing.JTextField campoEndereco;
    private javax.swing.JTextField campoEnderecoPaciente;
    private javax.swing.JTextField campoFiliacao;
    private javax.swing.JTextField campoHora;
    private javax.swing.JTextField campoLaudo;
    private javax.swing.JTextField campoLeito;
    private javax.swing.JTextField campoMunicipio;
    private javax.swing.JTextField campoNascimento;
    private javax.swing.JTextField campoNaturalidade;
    private javax.swing.JTextField campoNome;
    private javax.swing.JTextField campoNomeConjuge;
    private javax.swing.JTextField campoNomeResponsavel;
    private javax.swing.JTextField campoOrd;
    private javax.swing.JTextField campoProfissao;
    private javax.swing.JTextField campoProntuario;
    private javax.swing.JTextField campoTelefone;
    private javax.swing.JComboBox comboCivil;
    private javax.swing.JComboBox comboMotivo;
    private javax.swing.JComboBox comboSex;
    private javax.swing.JComboBox comboSimNao;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
    private classesBancodeDados.FichaInternamento ficha;
    private boolean editable;
    private Paciente paciente;
}
